package com.tao.com.xiaowen.module.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tao.com.xiaowen.config.ToastUtil;
import com.tao.com.xiaowen.config.http.MyJson;
import com.tao.com.xiaowen.config.http.OkHttpHelper;
import com.tao.com.xiaowen.model.News;
import com.tao.com.xiaowen.module.picture.PictureContent;
import com.tao.com.xiaowen.adapter.PictureFragmentAdapter;
import com.tao.com.xiaowen.model.Model;
import com.tao.com.xiaowen.model.Picture;
import com.tao.com.xiaowen.R;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2016/8/1.
 */
public class PictureFragment extends Fragment {
    private TextView titleView;

    private View view;
    private XRecyclerView recyclerView;
    private List<Picture> pictureList;
    private PictureFragmentAdapter pictureFragmentAdapter;

    private boolean isRefresh = false;
    private boolean isLoadMore = false;

    private int PAGE = 0;
    private String picUrl = Model.PICTURE_URL + "&pn=" + PAGE;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_picture, container, false);
        initView();

        return view;
    }

    private void initView() {
        titleView = (TextView) view.findViewById(R.id.title);
        recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerView);

        titleView.setText("图片");
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
//                picUrl = Model.PICTURE_URL + "&pn=" + PAGE;
//                OkHttpHelper.getInstance().getPicExecute(picUrl);
                BmobQuery<Picture> query = new BmobQuery<>();
                query.order("createdAt");
                query.findObjects(new FindListener<Picture>() {
                    @Override
                    public void done(List<Picture> list, BmobException e) {
                        if (e == null) {
                            for (Picture picData : list) {
                                pictureList.add(picData);
                            }
//                            ToastUtil.toast(getActivity(), "已经加载了最新的数据");
                            recyclerView.refreshComplete();
                            pictureFragmentAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.toast(getActivity(), e.getMessage());
                        }
                    }
                });
                isRefresh = true;
            }

            @Override
            public void onLoadMore() {
//                picUrl = Model.PICTURE_URL + "&pn=" + PAGE;
//                OkHttpHelper.getInstance().getPicExecute(picUrl);
                isLoadMore = true;
            }
        });

        recyclerView.setRefreshing(true);

        pictureList = new ArrayList<>();
        pictureFragmentAdapter = new PictureFragmentAdapter(getActivity(), pictureList);
        recyclerView.setAdapter(pictureFragmentAdapter);

        pictureFragmentAdapter.OnItemClick(new PictureFragmentAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                Picture picture = pictureList.get(position);
                String picUrl = picture.getImage_url();
                Intent intent = new Intent(getActivity(), PictureContent.class);
                intent.putExtra("picUrl", picUrl);
                startActivity(intent);
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 404) {
                Toast.makeText(getActivity(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                recyclerView.refreshComplete();
                recyclerView.loadMoreComplete();
            } else if (msg.what == 200) {
                String result = msg.obj.toString();
                if (!result.equals("")) {
                    List<Picture> list = MyJson.getPictureJsonList(result);
                    if (list != null) {
                        PAGE = PAGE + 27;
                        if (isRefresh) {
                            pictureList.clear();
                            isRefresh = false;
                        }
                        if (isLoadMore) {
                            isLoadMore = false;
                        }
                        for (Picture picture : list) {
                            pictureList.add(picture);
                        }
                    }
                }
                pictureFragmentAdapter.notifyDataSetChanged();
                recyclerView.refreshComplete();
                recyclerView.loadMoreComplete();
            }

        }
    };
}
